package com.study.main

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.core.app.NotificationManagerCompat
import com.module.compose.base.App
import com.module.compose.base.ComposeActivity
import com.module.compose.base.composable.CommonItem
import com.module.compose.base.composable.CommonPage
import com.module.compose.base.utils.ScreenBg
import com.module.compose.base.utils.ToastUtils
import com.module.compose.base.utils.goTo
import com.module.compose.base.viewModel.OurViewModel
import com.study.main.common.SPValue
import com.study.main.ui.recording.RecordingActivity
import com.study.main.ui.sp.SPActivity
import com.study.messagebus.core.MessageBus
import com.study.messagebus.Receive
import com.study.messagebus.Register
import com.study.zrouter.ZRouter
import com.zrouter.annotation.Route

/**
 * 应用的首页入口
 */
@Register
@Route("main/MainActivity")
class MainActivity : ComposeActivity<OurViewModel>() {

    private lateinit var exception: String

    //测试git分支合并,继续操作
    @Receive("main/MainActivity")
    fun receiveMsg(msg: Map<String, String>) {
        msg["key"]?.let {
            ToastUtils.show(it)
        }
    }

    private fun goToSetting() {
        val intent = Intent()
        when {
            Build.VERSION.SDK_INT >= 26 -> {// android 8.0引导
                intent.action = "android.settings.APP_NOTIFICATION_SETTINGS";
                intent.putExtra("android.provider.extra.APP_PACKAGE", packageName);
            }
            Build.VERSION.SDK_INT >= 21 -> { // android 5.0-7.0
                intent.action = "android.settings.APP_NOTIFICATION_SETTINGS";
                intent.putExtra("app_package", packageName);
                intent.putExtra("app_uid", applicationInfo.uid);
            }
            else -> {//其它
                intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS";
                intent.data = Uri.fromParts("package", packageName, null);
            }
        }
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
        startActivity(intent);
    }

    private val items =
        listOf("ZRouter", "compose", "Map", "SP数据保存", "Exception", "通知开启", "通话录音", "玩安卓")

    @Composable
    override fun ContentView() {
        CommonPage(title = "首页", isShowBack = false) {
            LazyColumn(Modifier.fillMaxSize()) {
                items(count = items.size) { index ->
                    CommonItem(
                        content = items[index],
                        iconId = R.drawable.icon_compose,
                        backgroundColor = ScreenBg
                    ) {
                        when (items[index]) {
                            "ZRouter" -> {
                                MessageBus.send(
                                    "businessOne/BusinessOneActivity",
                                    mapOf("key" to "从首页过来的MessageBus消息"),
                                    true
                                )
                                ZRouter.instance().routeTo(
                                    this@MainActivity,
                                    "businessOne/BusinessOneActivity",
                                    null
                                )
                            }
                            "compose" -> {
                                ZRouter.instance().routeTo(
                                    this@MainActivity,
                                    "sample/MainActivity",
                                    null
                                )
                            }
                            "Map" -> {
                                ZRouter.instance().routeTo(
                                    this@MainActivity,
                                    "module_map_sample/MapSampleActivity",
                                    null
                                )
                            }
                            "SP数据保存" -> {
                                SPValue.userName = "Zhang"
                                SPValue.age = 18
                                goTo(SPActivity::class.java)
                            }

                            "Exception" -> {
                                println(exception)
                            }
                            "通知开启" -> {
                                if (NotificationManagerCompat.from(App.getContext())
                                        .areNotificationsEnabled()
                                ) {
                                    Toast.makeText(App.getContext(), "通知已开启", Toast.LENGTH_SHORT)
                                        .show()
                                } else {
                                    Toast.makeText(App.getContext(), "通知未开启", Toast.LENGTH_SHORT)
                                        .show()
                                    goToSetting()
                                }
                            }
                            "通话录音" -> goTo(RecordingActivity::class.java)
                            "玩安卓" -> ZRouter.instance().routeTo(
                                this@MainActivity,
                                "wanAndroid/LoginActivity",
                                null
                            )
                        }
                    }
                }
            }
        }
    }
}